package com.ngc.mall.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ngc.mall.common.configure.mybatisPlus.BaseServiceImpl;
import com.ngc.mall.domain.PermissionDomain;
import com.ngc.mall.domain.PermissionMenuDomain;
import com.ngc.mall.dto.PermissionDTO;
import com.ngc.mall.mapper.PermissionMapper;
import com.ngc.mall.service.IPermissionService;
import com.ngc.mall.service.IPermissionMenuService;
import com.ngc.mall.vo.PermissionVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class PermissionServiceImpl extends BaseServiceImpl<PermissionMapper, PermissionDomain> implements IPermissionService {
    @Resource
    private IPermissionMenuService  iPermissionMenuService;
    @Override
    public List<PermissionVO> selectAllPermission() {
        List<PermissionDomain> permissionDomainList= super.list();
        List<PermissionVO> permissionVOList=permissionDomainList.stream().map(domain->{
            PermissionMenuDomain permissionMenuDomain=iPermissionMenuService.getOne(Wrappers.<PermissionMenuDomain>lambdaQuery().eq(PermissionMenuDomain::getPermissionId,domain.getId()));
            return PermissionVO
                    .builder()
                    .name(domain.getName())
                    .id(domain.getId())
                    .type(domain.getType())
                    .menuId(permissionMenuDomain.getMenuId())
                    .build();
        }).collect(Collectors.toList());
        return permissionVOList;
    }

    @Override
    public Boolean createPermission(PermissionDTO dto) {
        PermissionDomain domain=PermissionDomain.builder().name(dto.getName()).type(dto.getType()).build();
        Long id=super.create(domain);
        return iPermissionMenuService.save(PermissionMenuDomain.builder().permissionId(id).menuId(dto.getMenuId()).build());
    }

//    public Boolean updatePermission(PermissionDTO dto){
//
//    }
}
